<template>
  <div class="page">
    <r-header />
    <main class="main">
      <div class="header">
        <h1>考试记录</h1>
        <p v-if="testPapers.length === 0" class="empty-tip">暂无考试记录</p>
      </div>

      <div class="paper-list" v-if="testPapers.length > 0">
        <div 
          v-for="paper in testPapers" 
          :key="paper.tesrId"
          class="paper-item"
          @click="viewPaperDetail(paper.tesrId)"
        >
          <div class="paper-header">
            <div class="topic" :title="paper.topic">
              {{ paper.topic }}
            </div>
            <div class="date">{{ formatDate(paper.createdAt) }}</div>
          </div>
          <div class="paper-footer">
            <div class="info-item">
              <span class="label">Token消耗:</span>
              <span class="value">{{ paper.totalTokens || 0 }}</span>
            </div>
            <div class="info-item">
              <span class="label">正确率:</span>
              <span class="value" :class="getCorrectRateClass(paper.correctRate)">
                {{ formatCorrectRate(paper.correctRate) }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
export default {
  name: "TestHistory",
  data() {
    return {
      testPapers: []
    }
  },
  mounted() {
    this.loadTestPapers();
  },
  methods: {
    // 加载试卷列表
    async loadTestPapers() {
      const userId = this.$store.state.user?.profile?.id;
      
      if (!userId) {
        console.error('用户ID不存在');
        return;
      }

      try {
        const response = await fetch(`http://localhost:8088/api/test-paper/list/${userId}`);
        const result = await response.json();
        
        if (result.code === 200 && result.data && result.data.list) {
          console.log('获取到的试卷列表:', result.data.list);
          this.testPapers = result.data.list;
        } else {
          console.error('获取试卷列表失败:', result.msg);
        }
      } catch (error) {
        console.error('加载试卷列表失败:', error);
      }
    },

    // 查看试卷详情
    viewPaperDetail(paperId) {
      console.log('点击试卷，tesrId:', paperId);
      console.log('tesrId类型:', typeof paperId);
      console.log('tesrId值:', paperId);
      
      // 确保paperId不为空且为有效值
      if (!paperId) {
        console.error('paperId为空，无法跳转');
        alert('试卷ID无效，无法查看详情');
        return;
      }
      
      // 跳转到试卷详情页面
      this.$router.push({
        path: '/test-result',
        query: { paperId: paperId }
      });
    },

    // 格式化日期
    formatDate(dateStr) {
      if (!dateStr) return '';
      const date = new Date(dateStr);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}`;
    },

    // 格式化正确率
    formatCorrectRate(rate) {
      if (rate === null || rate === undefined) return '未完成';
      return `${rate.toFixed(1)}%`;
    },

    // 根据正确率返回CSS类名
    getCorrectRateClass(rate) {
      if (rate === null || rate === undefined) return 'rate-unfinished';
      if (rate >= 90) return 'rate-excellent';
      if (rate >= 80) return 'rate-good';
      if (rate >= 60) return 'rate-pass';
      return 'rate-fail';
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #334155 100%);
  min-height: 100vh;
  padding-top: 80px; // 为固定导航栏留出空间
  position: relative;
  
  // 添加动态背景粒子效果
  &::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: 
      radial-gradient(circle at 20% 50%, rgba(59, 130, 246, 0.08) 0%, transparent 50%),
      radial-gradient(circle at 80% 80%, rgba(59, 130, 246, 0.08) 0%, transparent 50%),
      radial-gradient(circle at 40% 20%, rgba(59, 130, 246, 0.05) 0%, transparent 50%);
    pointer-events: none;
    z-index: 0;
  }
}

.main {
  max-width: 900px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  position: relative;
  z-index: 1;
}

.header {
  text-align: center;
  margin-bottom: 2.5rem;

  h1 {
    color: #ffffff;
    font-size: 2.5rem;
    margin-bottom: 0.75rem;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    font-weight: 700;
  }

  .empty-tip {
    color: rgba(255, 255, 255, 0.8);
    font-size: 1.1rem;
    margin-top: 1rem;
  }
}

.paper-list {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.paper-item {
  // 毛玻璃质感效果 - 更透明
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 16px;
  padding: 1.75rem 2rem;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.15);

  &:hover {
    transform: translateY(-4px) scale(1.02);
    box-shadow: 
      0 20px 40px rgba(0, 0, 0, 0.15),
      inset 0 1px 0 rgba(255, 255, 255, 0.2);
    border-color: rgba(255, 255, 255, 0.3);
    background: rgba(255, 255, 255, 0.12);
  }

  &:active {
    transform: translateY(-2px) scale(1.01);
  }

  .paper-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.25rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.15);
    gap: 1rem;

    .topic {
      flex: 1;
      color: #ffffff;
      font-size: 1.15rem;
      font-weight: 600;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      text-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    }

    .date {
      color: rgba(255, 255, 255, 0.75);
      font-size: 0.85rem;
      white-space: nowrap;
      background: rgba(255, 255, 255, 0.1);
      padding: 0.3rem 0.75rem;
      border-radius: 20px;
      font-weight: 500;
    }
  }

  .paper-footer {
    display: flex;
    gap: 2rem;
    flex-wrap: nowrap; // 强制在一行显示

    .info-item {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      flex: 1; // 平均分配空间
      min-width: 0; // 允许收缩

      .label {
        color: rgba(255, 255, 255, 0.9);
        font-size: 0.95rem;
        font-weight: 500;
        white-space: nowrap; // 标签不换行
      }

      .value {
        font-weight: 700;
        font-size: 1.1rem;
        padding: 0.25rem 0.75rem;
        border-radius: 8px;
        color: #08262a;
        background: rgba(84, 170, 207, 0.2);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        white-space: nowrap; // 值不换行

        &.rate-excellent {
          color: #10b981;
          background: rgba(16, 185, 129, 0.2);
        }

        &.rate-good {
          color: #3b82f6;
          background: rgba(59, 130, 246, 0.2);
        }

        &.rate-pass {
          color: #f59e0b;
          background: rgba(245, 158, 11, 0.2);
        }

        &.rate-fail {
          color: #ef4444;
          background: rgba(239, 68, 68, 0.2);
        }

        &.rate-unfinished {
          color: rgba(255, 255, 255, 0.6);
          background: rgba(255, 255, 255, 0.1);
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .main {
    padding: 1.5rem 1rem;
  }

  .header {
    margin-bottom: 2rem;

    h1 {
      font-size: 2rem;
    }
  }

  .paper-item {
    padding: 1.25rem 1.5rem;

    .paper-header {
      // 保持同一行显示，通过flex布局适配
      flex-wrap: nowrap;
      gap: 0.75rem;
      padding-bottom: 0.75rem;

      .topic {
        font-size: 1.05rem;
        min-width: 0; // 允许flex收缩
      }

      .date {
        font-size: 0.75rem;
        flex-shrink: 0; // 日期标签不收缩
        padding: 0.25rem 0.6rem;
      }
    }

    .paper-footer {
      flex-wrap: nowrap; // 保持一行显示
      gap: 1rem;

      .info-item {
        flex: 1;
        min-width: 0; // 允许收缩

        .label {
          font-size: 0.85rem; // 移动端标签字体稍小
        }

        .value {
          font-size: 0.95rem; // 移动端值字体稍小
          padding: 0.2rem 0.6rem; // 移动端内边距稍小
        }
      }
    }
  }
}
</style>
